package com.frank.im.base;

import java.util.ArrayList;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.frank.im.bean.ImRespData;
import com.frank.im.utils.Logger;

/**
 * im通讯广播接收者
 * 
 * @author Frank
 * @version 1.0 Create by 2016.4.14
 */
public class ImBroadcast extends BroadcastReceiver {
	private final static String TAG = ImBroadcast.class.getSimpleName();
	public final static String RESP_TYPE_PUSH = "resp_type_push";
	public final static String RESP_TYPE_RESP = "resp_type_resp";
	public final static String OPERATE_TYPE_UN_READMSG = "operate_type_un_readmsg";
	public final static String OPERATE_TYPE_RECEIVE_MSG = "operate_type_receive_msg";
	public final static String OPERATE_TYPE_SEND_START = "operate_type_send_start";
	public final static String OPERATE_TYPE_SEND_FINISHED = "operate_type_send_finished";
	public final static String OPERATE_TYPE_SEND_FAILED = "operate_type_send_failed";
	public final static String OPERATE_TYPE_GET_USER_FINISHED = "operate_type_get_user_finished";
	public final static String OPERATE_TYPE_UN_READ_SYS_MSG = "operate_type_un_read_sys_msg";
	public final static String OPERATE_TYPE_NEW_SYS_MSG = "operate_type_new_sys_msg";
	private String respType = "";
	private String operateType = "";
	private ImRespData data = null;

	@Override
	public void onReceive(Context context, Intent intent) {
		try {
			respType = intent.getStringExtra("respType");
			operateType = intent.getStringExtra("operateType");
			data = (ImRespData) intent.getSerializableExtra("data");
			if (respType != null && !respType.equals("") && operateType != null) {
				Logger.i(TAG, "respType:" + respType);
				Logger.i(TAG, "operateType:" + operateType);
				switchEvent(respType, operateType, data);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void switchEvent(String respType, String operateType,
			ImRespData imRespData) {
		Logger.i(TAG, "1respType:" + respType);
		Logger.i(TAG, "1operateType:" + operateType);
		ImNotifierEvent event = new ImNotifierEvent();
		event.setEventCode(ImNotifierEvent.REC_MSG);
		if (imRespData != null) {
			event.setImRespData(imRespData);
		}
		if (respType.equals(RESP_TYPE_PUSH)) {
			switch (operateType) {
			case OPERATE_TYPE_RECEIVE_MSG:
				event.setEventCode(ImNotifierEvent.REC_MSG);
				break;

			default:
				break;
			}
		} else if (respType.equals(RESP_TYPE_RESP)) {
			switch (operateType) {
			case OPERATE_TYPE_SEND_FINISHED:
				event.setEventCode(ImNotifierEvent.SEND_MSG_SUCCESS);
				break;
			case OPERATE_TYPE_SEND_FAILED:
				event.setEventCode(ImNotifierEvent.SEND_MSG_FAILED);
				break;

			default:
				break;
			}
		}
		doOnEvent(event);
	}

	/**
	 * 执行回调
	 * 
	 * @param event
	 */
	private void doOnEvent(ImNotifierEvent event) {
		Logger.i(TAG, "开始遍历回调");
		ArrayList<ImEventListener> eventListeners = ImChatManage.getInstance()
				.getEventListeners();
		if (eventListeners != null && eventListeners.size() > 0) {
			Logger.i(TAG, "1开始遍历回调");
			for (ImEventListener eventListener : eventListeners) {
				Logger.i(TAG, "开始执行回调");
				eventListener.onEvent(event);
			}
		}
	}

}
